<?php
/*
* @author: Thiago S.F. - thiagosfsjc@gmail.com
* @example: 
* $tabela = new tabela("id=\"dados\"", 1);
* $tabela->setCapition("Tabela Gerada Dinamicamente Pela Classe <b>" . get_class($tabela) . "</b>" );
* $tabela->th( array( array("Nome", " class=\"cinza\""), "Idade") );
* $tabela->td( array("Joao", 25) );
* $tabela->td( array("Maria", 20), " class=\"vermelho\"" );
*/
class Tabela {

	private $thTotal;

	private $tdTotal;

	private $atributos;

	private $tabulcaoTotal;

	private $cssTableClass;

	public function __construct($cssClass = "", $atributos = "", $tabulcaoTotal = 0) {
		$this->atributos = $atributos;
		$this->tabulcaoTotal = $tabulcaoTotal;
		$this->cssTableClass = $cssClass;
	}

	private function tab($n) {
		$tabulacao = "";
		$n += $this->tabulcaoTotal;
		for($i = 0; $i < $n; $i ++) {
			@$tabulacao .= "\t";
		}
		return $tabulacao;
	}

	public function setCapition($caption, $cssClass) {
		$this->capition = $this->tab(1) . "<caption class='$cssClass'>{$caption}</caption>\n";
	}

	public function th(array $args, $atributosTh = "") {
		$this->thTotal = $this->tab(1) . "<thead>\n";
		$this->thTotal .= $this->tab(2) . "<tr" . $atributosTh . ">\n";
		foreach($args as $valor) {
			if(gettype($valor) == "array") {
				$this->thTotal .= $this->tab(3) . "<th" . $valor[1] . ">" . $valor[0] . "</th>\n";
			} else {
				$this->thTotal .= $this->tab(3) . "<th $atributosTh>{$valor}</th>\n";
			}
		}
		$this->thTotal .= $this->tab(2) . "</tr>\n";
		$this->thTotal .= $this->tab(1) . "</thead>\n";
	}

	public function td(array $args, $atributosTd = "") {
		@$this->tdTotal .= $this->tab(2) . "<tr" . $atributosTd . ">\n";
		foreach($args as $valor) {
			if(gettype($valor) == "array")
				$this->tdTotal .= $this->tab(3) . "<td" . $valor[1] . ">" . $valor[0] . "</td>\n";
			else
				$this->tdTotal .= $this->tab(3) . "<td $atributosTd>{$valor}</td>\n";
		}
		$this->tdTotal .= $this->tab(2) . "</tr>\n";
	}

	public function montarTabela() {
		$tabela .= $this->tab(0) . "<table class='$this->cssTableClass' " . $this->atributos . ">\n";
		$tabela .= $this->capition;
		$tabela .= $this->thTotal;
		$tabela .= $this->tab(1) . "<tbody>\n";
		$tabela .= $this->tdTotal;
		$tabela .= $this->tab(1) . "</tbody>\n";
		$tabela .= $this->tab(0) . "</table>\n";
		return $tabela;
	}

	public function exibirTabela() {
		echo $this->montarTabela();
	}
}
?>